<?php

namespace App\Services;

/**
 * Created by PhpStorm.
 * User: omybug
 * Date: 17/5/29
 * Time: 21:18
 */
class Util
{

    public static function getTimeId(){
        $s = time() - strtotime(date("Y-m-d 00:00:00",time()));
        return date("ymd",time()).str_pad($s,5,"0",STR_PAD_LEFT).mt_rand(100,999);
    }

    public static function password($password, $solt = 'rent-server')
    {
        return md5($password . $solt);
    }

    public static function httpGet($url, $data = [])
    {
        if (!empty($data)) {
            $url = $url . '?' . http_build_query($data);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    public static function httpsGetJson($url,$data=[])
    {
        return json_decode(self::httpGet($url, $data),true);
    }

    public static function httpsPostJson($url,$data)
    {
        return json_decode(self::httpPost($url,$data), true);
    }

    public static function httpPost($url, $data = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data));
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_POST, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    public static function checkPhone($phone){
        return preg_match("/^1\d{10}$/",$phone);
    }

    public static function getAgeByBirth($birth)
    {
        list($year, $month, $day) = explode("-", $birth);
        $year_diff = date("Y") - $year;
        $month_diff = date("m") - $month;
        $day_diff = date("d") - $day;
        if ($day_diff < 0 || $month_diff < 0)
            $year_diff--;
        return $year_diff;
    }

    public static function getBirthByAge($age)
    {
        $year = date("Y") - $age;
        return $year . '-01-01';
    }


    public static function isMobile(){
        $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
        $useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';
        function CheckSubstrs($substrs,$text){
            foreach($substrs as $substr)
                if(false!==strpos($text,$substr)){
                    return true;
                }
            return false;
        }
        $mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');
        $mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod','iPad');

        $found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock) ||
            CheckSubstrs($mobile_token_list,$useragent);

        if ($found_mobile){
            return true;
        }else{
            return false;
        }
    }

    public static function obj2Array($obj)
    {
        return json_decode(json_encode($obj,JSON_UNESCAPED_UNICODE),true);
    }

    public static function array2Obj($arr)
    {
        return json_decode(json_encode($arr));
    }

    public static function domain($url)
    {
        return str_replace(url(''),'',$url);
    }

    public static function trimAll($str){
        $str = str_replace("&nbsp;","",strip_tags($str));
        $rule = array(" ","　","\t","\n","\r");
        return str_replace($rule, '', $str);
    }

    public static function formatTime($str){
        $time = strtotime($str);
        return gmstrftime("%d %b %Y",$time);
    }

    public static function getDate($pattern,$str){
        $time = strtotime($str);
        return date($pattern,$time);
    }

    public static function getMonth($date){
        $firstday = date("Y-m-01",strtotime($date));
        $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day"));
        return array($firstday,$lastday);
    }

    public static function getlastMonthDays($date){
        $timestamp=strtotime($date);
        $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));
        $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
        return array($firstday,$lastday);
    }

    public static function getNextMonthDays($date){
        $timestamp=strtotime($date);
        $arr=getdate($timestamp);
        if($arr['mon'] == 12){
            $year=$arr['year'] +1;
            $month=$arr['mon'] -11;
            $firstday=$year.'-0'.$month.'-01';
            $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
        }else{
            $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)+1).'-01'));
            $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
        }
        return array($firstday,$lastday);
    }

    public static function chineseDate($str){
        $arr = explode('-',$str);
        $tmpstr = '';
        if(!empty($arr[0])){
            $tmpstr.=$arr[0].'年';
        }
        if(!empty($arr[1])){
            $tmpstr.=$arr[1].'月';
        }
        if(!empty($arr[2])){
            $tmpstr.=$arr[2].'日';
        }
        return $tmpstr;
    }

}